﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.Global;
using CE.Database;

public partial class carinsurance_renewal_renew_check : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!GetExistingLead(Request.QueryString["custId"].ToString(), Request.QueryString["carId"].ToString(), Session[CE.Global.General.UserId].ToString(), Request.QueryString["refno"].ToString()))
            {
                callFunction(Request.QueryString["custId"].ToString(), Request.QueryString["carId"].ToString(), Session[CE.Global.General.UserId].ToString(), Request.QueryString["refno"].ToString());
            }
        }
    }
    //http://localhost:8081/carinsurance/renewal/renew-check.aspx?custId=13587&carId=94283&refno=KSK942HC
    public void callFunction(string CustomerID, string CarId, string AgentId, string RefNo)
    {
        try
        {
            string _spName = "sprocCXCRMAgentRenewalLeadDetailsSelectSingleItem";
            MyDatabase _db = new MyDatabase();
            DataTable _dtCallMeAgentLeads = new DataTable("tblRenealAgentLeads");
            SortedList _paraList = new SortedList();
            _paraList.Add("@CustomerID", CustomerID);
            _paraList.Add("@CarID", CarId);
            _paraList.Add("@CallType", 100);
            _paraList.Add("@AgentId", AgentId);
            _dtCallMeAgentLeads = _db.GetDataTableBySP(_spName, _paraList);

            string outAgentLeadID = "0", outCustomerID = "0", outCarID = "0";
            if (_dtCallMeAgentLeads != null && _dtCallMeAgentLeads.Rows.Count > 0)
            {
                outAgentLeadID = _dtCallMeAgentLeads.Rows[0]["AgentLeadId"].ToString();
                outCustomerID = _dtCallMeAgentLeads.Rows[0]["CustomerId"].ToString();
                outCarID = _dtCallMeAgentLeads.Rows[0]["CarId"].ToString();
            }

            Response.Redirect("../offline/ViewCINoMatchFound.aspx?leadid=" + outAgentLeadID + "&calltype=100&renew=2&custid=" + outCustomerID + "&carid=" + outCarID + "&refno=" + RefNo);
        }
        catch (Exception)
        {
            lblError.Text = "ไม่สามารถไปหน้าใบงานได้ !";
        }
    }

    private bool GetExistingLead(string CustomerID, string CarID, string AgentId, string RefNo)
    {
        try
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblNewCall");
            string _strReturnMessage = string.Empty;

            string _spName = "sprocCXCRMCheckRenewRegularCall";
            SortedList _paraList = new SortedList();
            _paraList.Add("@CustomerID", CustomerID);
            _paraList.Add("@CarID", CarID);
            _paraList.Add("@AgentID", AgentId);
            //if (lblCallType.Text == "102" && lblAgentLeadID.Text != string.Empty && lblAgentLeadID.Text != "0")
            //{
            //    _paraList.Add("@pAgentLeadID", lblAgentLeadID.Text);
            //}
            _paraList.Add("@pCallType", "100");

            _dt = _db.GetDataTableBySP(_spName, _paraList);

            Int16 _callStatus = 0;
            Int16 _callType = 0;
            if (_dt != null && _dt.Rows.Count > 0)
            {
                if (_dt.Rows[0].Table.Columns.Contains("OtherAgentID"))
                {
                    System.Text.StringBuilder _strReturnMessageBuilder = new System.Text.StringBuilder();
                    _strReturnMessageBuilder.Append("<table bgcolor='#ffffcc' width='450' cellpadding='5' cellspacing='5'>");
                    _strReturnMessageBuilder.Append("<tr><td colspan='3'><b style='color:red;'>" + Resources.General.FOUND + ":" + Resources.ResultPage.SmsgSorryFoundOtherAgentRecord + "</b></tr>");
                    _strReturnMessage += Resources.ResultPage.SmsgAgent + ": <b>" + _dt.Rows[0]["OtherAgentName"].ToString() + "</b>";
                    _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Broker + "</td><td>:</td><td><b>" + _dt.Rows[0]["OtherAgentName"].ToString() + "</b></td></tr>");

                    _callType = Convert.ToInt16(_dt.Rows[0]["CallType"].ToString());
                    if (_callType == 100)
                    {
                        //_strReturnMessage += " Type: <b style='color:green;'>REGULAR</b>";
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.CallType + "</td><td>:</td><td><b style='color:green;'>" + Resources.General.FirstFollowUpCall + "</b></td></tr>");
                    }
                    else if (_callType == 101)
                    {
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.CallType + "</td><td>:</td><td><b style='color:red;'>" + Resources.General.CallMeRequest + "</b></td></tr>");
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgTime + "</td><td>:</td><td><b>" + _dt.Rows[0]["CallTime"].ToString() + "</b></td></tr>");
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.CarInsurance.CompanyName + "</td><td>:</td><td><b>" + _dt.Rows[0]["ProviderCompanyName"].ToString() + "</b></td></tr>");
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgRequestOn + "</td><td>:</td><td><b>" + _dt.Rows[0]["CallMeRequestDateTime"].ToString() + "</b></td></tr>");
                    }

                    _callStatus = Convert.ToInt16(_dt.Rows[0]["CallStatus"].ToString());
                    if (_callStatus == 1)
                    {
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Status + "</td><td>:</td><td><b style='color:green;'>" + Resources.General.Pending + "</b></td></tr>");
                    }
                    else if (_callStatus == 2)
                    {
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Status + "</td><td>:</td><td><b style='color:red;'>" + Resources.General.Callback + "</b></td></tr>");
                    }
                    else if (_callStatus == 3)
                    {
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Status + "</td><td>:</td><td><b style='color:blue;'>" + Resources.General.FollowUp + "</b></td></tr>");
                    }

                    if (_dt.Rows[0]["OtherAgentCallBackDateTime"] != DBNull.Value)
                    {
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Callback + " " + Resources.General.DateTime + "</td><td>:</td><td><b>" + Convert.ToDateTime(_dt.Rows[0]["OtherAgentCallBackDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b></td></tr>");
                    }
                    if (_dt.Rows[0]["ModifiedDateTime"] != DBNull.Value)
                    {
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.DateTime + "</td><td>:</td><td><b>" + Convert.ToDateTime(_dt.Rows[0]["ModifiedDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b></td></tr>");
                    }

                    //Sup Wan - Br2
                    if (Session[CE.Global.General.UserId].ToString() != "34173")
                    {
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgTakeOver + "</td><td>:</td><td><a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallNow + "\");' href='../offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["OtherAgentLeadID"].ToString() + "&carid=" + CarID + "&custid=" + _dt.Rows[0]["OtherAgentCustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "&renew=2&otheragentid=" + _dt.Rows[0]["OtherAgentID"].ToString() + "'><b>[" + Resources.ResultPage.SmsgCallNow + "]</b></a></td>");
                    }
                    else
                    {
                        string _spNameSup = "sprocCXCRMAgentRenewalLeadDetailsSelectSingleItem";
                        MyDatabase _dbSup = new MyDatabase();
                        DataTable _dtCallMeAgentLeads = new DataTable("tblRenealAgentLeads");
                        SortedList _paraListSup = new SortedList();
                        _paraListSup.Add("@CustomerID", CustomerID);
                        _paraListSup.Add("@CarID", CarID);
                        _paraListSup.Add("@CallType", 100);
                        _paraListSup.Add("@AgentId", _dt.Rows[0].Table.Columns.Contains("OtherAgentID"));
                        _dtCallMeAgentLeads = _dbSup.GetDataTableBySP(_spNameSup, _paraListSup);

                        string outAgentLeadID = "0", outCustomerID = "0", outCarID = "0";
                        if (_dtCallMeAgentLeads != null && _dtCallMeAgentLeads.Rows.Count > 0)
                        {
                            outAgentLeadID = _dtCallMeAgentLeads.Rows[0]["AgentLeadId"].ToString();
                            outCustomerID = _dtCallMeAgentLeads.Rows[0]["CustomerId"].ToString();
                            outCarID = _dtCallMeAgentLeads.Rows[0]["CarId"].ToString();
                        }

                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgTakeOver + "</td><td>:</td><td><a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallNow + "\");' href='../offline/ViewCINoMatchFound.aspx?" + "leadid=" + outAgentLeadID + "&calltype=100&renew=2&custid=" + outCustomerID + "&carid=" + outCarID + "&refno=" + RefNo + "'><b>[" + Resources.ResultPage.SmsgCallNow + "]</b></a></td>");
                    }
                    
                    _strReturnMessageBuilder.Append("</table>");

                    lblNewCallMessage.Text = _strReturnMessageBuilder.ToString();
                    
                }
                else if (_dt.Rows[0].Table.Columns.Contains("AgentLeadID"))
                {
                    _callStatus = 1;
                    if (_dt.Rows[0].Table.Columns.Contains("CallStatus"))
                    {
                        if (_dt.Rows[0]["CallStatus"] != DBNull.Value)
                        {
                            _callStatus = Convert.ToInt16(_dt.Rows[0]["CallStatus"].ToString());
                        }
                    }

                    _callType = Convert.ToInt16(_dt.Rows[0]["CallType"].ToString());

                    if (_callStatus == 100)
                    {
                        _strReturnMessage += "<br/><b style='color:red;'>" + Resources.ResultPage.SmsgThisCarCloased + "</b>";
                        _strReturnMessage += "&nbsp;<a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallAgain + "\");' href='../offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + CarID + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "&renew=2&refno=" + RefNo + "'><b>[" + Resources.ResultPage.SmsgCallAgainNow + "]</b></a>";
                        lblNewCallMessage.Text = _strReturnMessage;
                    }
                    else
                    {
                        if (_callType == 101)
                        {
                            _strReturnMessage += Resources.ResultPage.SmsgType + " : <b style='color:red;'>" + Resources.ResultPage.SmsgCallMe + "</b>";
                            _strReturnMessage += Resources.ResultPage.SmsgTime + " : <b>" + _dt.Rows[0]["CallTime"].ToString() + "</b>";
                            _strReturnMessage += Resources.ResultPage.SmsgCompany + " : <b>" + _dt.Rows[0]["ProviderCompanyName"].ToString() + "</b>";
                            _strReturnMessage += Resources.ResultPage.SmsgRequestOn + " : <b>" + Convert.ToDateTime(_dt.Rows[0]["CallMeRequestDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b>";
                            _strReturnMessage += "&nbsp;<a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallNow + "\");' href='../offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + CarID + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "&renew=2&refno=" + RefNo + "'><b>[" + Resources.ResultPage.SmsgCallNow + "]</b></a>";
                            lblNewCallMessage.Text = _strReturnMessage;
                        }
                        else if (_callType == 102)
                        {
                            _strReturnMessage += Resources.ResultPage.SmsgType + " : <b style='color:red;'>CALL ME BACK</b>";
                            _strReturnMessage += Resources.ResultPage.SmsgTime + " : <b>" + _dt.Rows[0]["CallTime"].ToString() + "</b>";
                            _strReturnMessage += Resources.ResultPage.SmsgCompany + " : <b>" + _dt.Rows[0]["ProviderCompanyName"].ToString() + "</b>";
                            _strReturnMessage += Resources.ResultPage.SmsgRequestOn + " : <b>" + Convert.ToDateTime(_dt.Rows[0]["CallMeRequestDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b>";
                            _strReturnMessage += "&nbsp;<a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallNow + "\");' href='../offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + CarID + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&callmeid=" + _dt.Rows[0]["CallMeID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "&renew=2&refno=" + RefNo + "'><b>[" + Resources.ResultPage.SmsgCallNow + "]</b></a>";
                            lblNewCallMessage.Text = _strReturnMessage;
                        }
                        else
                        {
                            _strReturnMessage = "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + CarID + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "&renew=2&refno=" + RefNo;
                            Response.Redirect("../offline/ViewCINoMatchFound.aspx?" + _strReturnMessage);
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
